/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_RTICONNEXTDDS_DAL_DOMAINPARTICIPANT_IMPL_HPP_
#define _DAL_RTICONNEXTDDS_DAL_DOMAINPARTICIPANT_IMPL_HPP_

#include <chrono>
#include <thread>
#include "ndds/ndds_c.h"
#include "dal_rticonnextdds/utils/ReferenceCount.hpp"
#include "dal_rticonnextdds/listener_impl/DomainParticipantListenerImpl.hpp"

struct rti_participant_monitor_callback_info_s {
    dal_participant_monitor_callback callback;
    void *data;
};

class DalDomainParticipantImpl
{
public:
    explicit DalDomainParticipantImpl(DDS_DomainParticipant *rti_participant,
                                      DomainParticipantListenerImpl* listener);
    ~DalDomainParticipantImpl();

    DDS_DomainParticipant* RtiddsDomainParticipant();
    dal_ret_t SetDomainFromConf(DDS_DomainParticipant *domain_from_conf);
    dal_ret_t DestroyDomainFromConf();

    void set_participant_monitor(std::thread &th);
    void enable_participant_monitor();
    void disable_participant_monitor();
    std::atomic_bool& get_participant_monitor_flag();
    dal_participant_monitor_callback get_monitor_callback();
    void* get_monitor_callback_data();

private:
    DomainParticipantListenerImpl* listener_ = nullptr;
    DDS_DomainParticipant *domain_from_conf_ = nullptr;
    DDS_DomainParticipant *rti_participant_ = nullptr;

    rti_participant_monitor_callback_info_s monitor_callback_info_;
    std::thread participant_monitor_thread_;
    std::atomic_bool participant_monitor_thread_flag_;
};

#endif  // _DAL_RTICONNEXTDDS_DAL_DOMAINPARTICIPANT_IMPL_HPP_